package com.lz.reflect.introspector;

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * IntrospectorDemo01.. 内省
 *
 * BeanUtils#populate
 * @author Lizhong
 * @date 2019/6/13
 */
public class IntrospectorDemo01 {
    public static void main(String[] args) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
        //
        // for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        //     System.out.println(propertyDescriptor);
        // }
        test2();
    }
    public static  void  test2() throws Exception{
        //Student s = new Student();
        //利用反射生成对象
        //com.cj.study.introspector.Student该参数可以配置到配置文件里，这才是我们想要的
        //是不是很熟悉？很多框架都是这么做的
        Class clazz = Class.forName("com.lz.reflect.introspector.Student");
        Student s = (Student)clazz.getConstructors()[0].newInstance();
        PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
        Method m = pd.getReadMethod();//得到getName()方法
        String value = (String)m.invoke(s, null);//调用getName()方法
        System.out.println("调用get方法得到name的值："+value);
        //改变name的值
        Method m1 = pd.getWriteMethod();//得到setName()方法
        m1.invoke(s, "李四");//调用setName()方法去修改name的值
        System.out.println("调用set方法改变name的值："+s.getName());
    }
    public  static void  test(){

    }
}
